package com.yuwenxin.bean.queryModel;

import com.yuwenxin.bean.User;
import com.yuwenxin.core.BaseQueryModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户的查询模型类
 * 保存查询条件的值
 * 继承实体类，原因是条件值肯定是某个属性的值
 * 实现BaseQueryModel接口，可以作为带条件查询的方法的参数
 */
public class UserQueryModel extends User implements BaseQueryModel {
    //继承了User类，具备了User的所有属性setter/getter方法
    private String passwordConfirm;
    private Map<String, String> errors = new HashMap<String, String>();

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public void setErrorMsg(String err,String errMsg){
        if ((err != null) && (errMsg != null)){
            errors.put(err,errMsg);
        }
    }

    public Map<String,String> getErrors(){
        return errors;
    }

    public boolean login_validate(String pswd){
        boolean flag = true;
        if (this.getName() == null || this.getName().trim().equals("")){
            errors.put("name", "请输入姓名");
            flag = false;
        }

        if (pswd == null || pswd.trim().equals("")){
            errors.put("password","请输入密码");
            flag = false;
        }else if (pswd.length() > 20 || pswd.length() < 6){
            errors.put("password","请输入6-20位字符");
            flag = false;
        }

        return flag;
    }

//    public boolean normal_pswd_validate(String pswd){
//        boolean flag = true;
//
//        if (pswd == null || pswd.trim().equals("")){
//            errors.put("password","请输入密码");
//            flag = false;
//        }else if (pswd.length() > 20 || pswd.length() < 6){
//            errors.put("password","请输入6-20位字符");
//            flag = false;
//        }
//
//        return flag;
//    }

    public boolean regist_validate(String pswd){
        boolean flag;
        flag = login_validate(pswd);

        if (pswd != null && !pswd.equals(passwordConfirm)){
            System.out.println(this.getPassword());
            System.out.println(passwordConfirm);
            System.out.println(this.getPassword().equals(passwordConfirm));
            errors.put("passwordConfirm","确认密码失败");
            flag = false;
        }
        if (this.getEmail() == null || this.getEmail().trim().equals("")) {
            errors.put("email", "请输入邮箱");
            flag = false;
        }else if (!this.getEmail()
                .matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)+")){
            errors.put("email","邮箱格式错误");
            flag = false;
        }

        if (this.getSex() == null || this.getSex().trim().equals("")){
            errors.put("sex","请选择你的性别");
            flag = false;
        }

        return flag;
    }
}
